package com.itheima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.constans.user.UserConstans;
import com.itheima.model.article.pojos.ApAuthor;
import com.itheima.model.common.admin.pojos.AdUser;
import com.itheima.model.common.dtos.PageResponseResult;
import com.itheima.model.common.dtos.ResponseResult;
import com.itheima.model.common.enums.AppHttpCodeEnum;
import com.itheima.model.media.pojos.WmUser;
import com.itheima.model.user.dtos.AuthDto;
import com.itheima.model.user.pojos.ApUser;
import com.itheima.model.user.pojos.ApUserRealname;
import com.itheima.user.feign.ArticleFeign;
import com.itheima.user.feign.WemediaFeign;
import com.itheima.user.mapper.ApUserMapper;
import com.itheima.user.mapper.ApUserRealnameMapper;
import com.itheima.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {


    @Override
    public ResponseResult loadListByStatus(AuthDto authDto) {
        //1.检查参数
        if (authDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.分页检查
        authDto.checkParam();
        //3分页查询 判断是状态码否为空
        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (authDto.getStatus()!=null){
            lambdaQueryWrapper.eq(ApUserRealname::getStatus,authDto.getStatus());
        }
        Page pageParam = new Page(authDto.getPage(), authDto.getSize());
        IPage page = page(pageParam, lambdaQueryWrapper);

        PageResponseResult responseResult = new PageResponseResult(authDto.getPage(), authDto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());//查找的数据

        return responseResult;
    }

    @Override
    @GlobalTransactional
    public ResponseResult updateStatusById(AuthDto authDto, Short statu) {
        //判断参数是否有效
        if (authDto==null||authDto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查status
        if (statu==null||(!statu.equals(UserConstans.FAIL_AUTH)&&!statu.equals(UserConstans.PASS_AUTH))){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //修改参数
        ApUserRealname apUserRealname =new ApUserRealname();
        apUserRealname.setId(authDto.getId());
        apUserRealname.setStatus(statu);
        if (authDto.getMsg()!=null){
            apUserRealname.setReason(authDto.getMsg());
        }
        updateById(apUserRealname);
        //审核通过状态需创建自媒体和制作者
        if (statu.equals(UserConstans.PASS_AUTH)){
            ResponseResult responseResult = createWmUserAndAuthor(authDto);
            if (responseResult!=null){
                return responseResult;
            }
        }
        //int a= 2/0;
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private WemediaFeign wemediaFeign;

    public ResponseResult createWmUserAndAuthor(AuthDto authDto) {
        //获取用户创建自媒体
        Integer ApUserRealnameId = authDto.getId();
        ApUserRealname apUserRealname = getById(ApUserRealnameId);
        Integer userId = apUserRealname.getUserId();
        ApUser apUser = apUserMapper.selectById(userId);
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ResponseResult byName = wemediaFeign.findByName(apUser.getName());
       /* if (byName.getData()==null){
            return byName;
        }*/
        //WmUser wmUser = (WmUser) byName.getData();//查重名?  那我是不是可以不用转型
        WmUser wmUser = new WmUser();
        if (byName.getData()==null){//data 16?
            wmUser.setApUserId(apUser.getId());
            wmUser.setCreatedTime(new Date());
            wmUser.setName(apUser.getName());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setPhone(apUser.getPhone());
            wmUser.setStatus(9);
            wemediaFeign.save(wmUser);
        }
        //创建作者信息
        ResponseResult responseResult = createAuthor(wmUser);
        if (responseResult!=null){
            return responseResult;
        }
        apUser.setFlag((short)1);
        apUserMapper.updateById(apUser);
        return  null;
    }
    @Autowired
    private ArticleFeign articleFeign;
    private ResponseResult createAuthor(WmUser wmUser) {
        Integer apUserId = wmUser.getApUserId();
        ResponseResult byUserId = articleFeign.findByUserId(apUserId);
        /*if (byUserId==null){
            return byUserId;
        }*/
        //ApAuthor apAuthor = (ApAuthor) byUserId.getData();
        ApAuthor apAuthor = new ApAuthor();
        if (byUserId.getData()==null){
            apAuthor.setName(wmUser.getName());
            apAuthor.setCreatedTime(new Date());
            apAuthor.setUserId(apUserId);
            apAuthor.setType(UserConstans.AUTH_TYPE);
            articleFeign.save(apAuthor);
        }
        return null;
    }


}
